package com.xmy.study.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.study.entity.EduTask;
import com.xmy.study.service.IEduTaskService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 任务表 前端控制器
 * </p>
 *
 * @since 2024-03-20
 */
@RestController
@RequestMapping("/api/outside/eduTask")
public class EduTaskController {
    @Autowired
    @Lazy
    IEduTaskService eduTaskService;

    /**
     * 获取课次列表，用于查询学生数据
     * @return
     */
    @ApiOperation("获取课次列表")
    @RequestMapping(value = "/getTaskNumber",method = RequestMethod.POST)
    public List<EduTask> getTaskNumber(@RequestBody Map<String, Object> reMap) {
        QueryWrapper<EduTask> eduTaskQueryWrapper = new QueryWrapper<>();
        if (reMap.get("classTypeId") != null && !reMap.get("classTypeId").toString().isEmpty()) {
            eduTaskQueryWrapper.eq("class_type_id", reMap.get("classTypeId"));
        }
        if (reMap.get("yearClassId") != null && !reMap.get("yearClassId").toString().isEmpty()) {
            eduTaskQueryWrapper.eq("year_class_id", reMap.get("yearClassId"));
        }
        if (reMap.get("yearPart") != null && !reMap.get("yearPart").toString().isEmpty()) {
            eduTaskQueryWrapper.eq("year_part", reMap.get("yearPart"));
        }
        if (reMap.get("quarterNum") != null && !reMap.get("quarterNum").toString().isEmpty()) {
            eduTaskQueryWrapper.eq("quarter_num", reMap.get("quarterNum"));
        }
        if (reMap.get("taskDate") != null && !reMap.get("taskDate").toString().isEmpty()) {
            eduTaskQueryWrapper.eq("task_date", reMap.get("taskDate"));
        }
        eduTaskQueryWrapper.eq("deleted",0);
        eduTaskQueryWrapper.orderByAsc("task_date");
        eduTaskQueryWrapper.groupBy("task_date");
        return eduTaskService.list(eduTaskQueryWrapper);
    }

    /**
     * 任务日期-实现：先拿出分班学生数据，for拿用半角逗号分开的字符串日期，在来查本接口
     * @return
     **/
    @ApiOperation("关联课次，拿学习数量与第几课次")
    @RequestMapping(value = "/getTaskList",method = RequestMethod.POST)
    public List<EduTask> getTaskList(@RequestBody Map<String, Object> reMap) {
        Object[] taskDate = new Object[0];
        if (reMap.get("taskDate") != null && !reMap.get("taskDate").toString().isEmpty()) {
            taskDate = reMap.get("taskDate").toString().split(",");
        }
        QueryWrapper<EduTask> eduTaskQueryWrapper = new QueryWrapper<>();
        if (taskDate.length > 0) {
            eduTaskQueryWrapper.in("task_date", taskDate);
        }
        String schoolDate = (String) reMap.get("schoolDate");
        if (schoolDate != null && !schoolDate.isEmpty()) {
            eduTaskQueryWrapper.le("task_date", schoolDate);
        }
        eduTaskQueryWrapper.eq("deleted",0);
        eduTaskQueryWrapper.orderByAsc("task_course_num");
        return eduTaskService.list(eduTaskQueryWrapper);
    }
}
